<html>
<head>
{php}html_header_show();{/php}

<link rel="stylesheet" href="{php}echo $GLOBALS['assets_static_relative'];{/php}/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">

<script type="text/javascript">
var mypcc = '{php} echo $GLOBALS['phone_country_code'] {/php}';
{literal}
// Only load jquery if not already closing. This page serves two purposes now,
//  for entring of vitals and for graphing/trending previous vitals by being embedded
//  in the interface/patient_file/encounter/trend_form.php page.
if (typeof jQuery == 'undefined') {
  document.write("<script type='text/javascript' src='{/literal}{php}echo $GLOBALS['assets_static_relative'];{/php}{literal}/jquery-min-1-7-2/index.js'><\/script>{/literal}")
}
</script>

<script type="text/javascript" src="{$FORM_ACTION}/library/dialog.js?v={php} echo $v_js_includes; {/php}"></script>
<script type="text/javascript" src="{$FORM_ACTION}/library/textformat.js?v={php} echo $v_js_includes; {/php}"></script>
<script type='text/javascript' src="{php}echo $GLOBALS['assets_static_relative'];{/php}/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>

{literal}
<script type="text/javascript">
function vitalsFormSubmitted() {
	var invalid = "";

	var elementsToValidate = new Array();

	elementsToValidate[0] = new Array();
	elementsToValidate[0][0] = 'weight_input';
	elementsToValidate[0][1] = '{/literal}{xl t="Weight"}{literal}' + ' (' + '{/literal}{xl t="lbs"}{literal}' + ')';

	elementsToValidate[1] = new Array();
	elementsToValidate[1][0] = 'weight_input_metric';
	elementsToValidate[1][1] = '{/literal}{xl t="Weight"}{literal}' + ' (' + '{/literal}{xl t="kg"}{literal}' + ')';

	elementsToValidate[2] = new Array();
	elementsToValidate[2][0] = 'height_input';
	elementsToValidate[2][1] = '{/literal}{xl t="Height/Length"}{literal}' + ' (' + '{/literal}{xl t="in"}{literal}' + ')';

	elementsToValidate[3] = new Array();
	elementsToValidate[3][0] = 'height_input_metric';
	elementsToValidate[3][1] = '{/literal}{xl t="Height/Length"}{literal}' + ' (' + '{/literal}{xl t="cm"}{literal}' + ')';

	elementsToValidate[4] = new Array();
	elementsToValidate[4][0] = 'bps_input';
	elementsToValidate[4][1] = '{/literal}{xl t="BP Systolic"}{literal}';

	elementsToValidate[5] = new Array();
	elementsToValidate[5][0] = 'bpd_input';
	elementsToValidate[5][1] = '{/literal}{xl t="BP Diastolic"}{literal}';

	for (var i = 0; i < elementsToValidate.length; i++) {
		var current_elem_id = elementsToValidate[i][0];
		var tag_name = elementsToValidate[i][1];

		document.getElementById(current_elem_id).classList.remove('error');

		if (isNaN(document.getElementById(current_elem_id).value)) {
			invalid += "{/literal}{xl t="The following field has an invalid value"}{literal}" + ": " + tag_name + "\n";
			document.getElementById(current_elem_id).className = document.getElementById(current_elem_id).className + " error";
			document.getElementById(current_elem_id).focus();
		}
	}

	if (invalid.length > 0) {
		invalid += "\n" + "{/literal}{xl t="Please correct the value(s) before proceeding!"}{literal}";
		alert(invalid);

		return false;
	} else {

		return top.restoreSession();
	}
}
</script>
<style type="text/css" title="mystyles" media="all">
.title {
	font-size: 120%;
	font-weight: bold;
}
.currentvalues {
    border-right: 1px solid black;
    padding-right:5px;
    text-align: left;
}
.valuesunfocus {
    border-right: 1px solid black;
    padding-right:5px;
    background-color: #ccc;
    text-align: left;
}
.unfocus {
    background-color: #ccc;
}
.historicalvalues {
    background-color: #ccc;
    border-bottom: 1px solid #ddd;
    border-right: 1px solid #ddd;
    text-align: right;
}
table {
    border-collapse: collapse;
}
td,th {
    padding-right: 10px;
    padding-left: 10px;
}
.hide {
    display:none;
}
.readonly {
    display:none;
}

.error {
  border:2px solid red;
}
</style>
{/literal}

</head>
<body bgcolor="{$STYLE.BGCOLOR2}">
<p><table><tr><td><span class="title">{xl t="Vitals"}</span></td><td>&nbsp;&nbsp;&nbsp;<a href="../summary/demographics.php" class="readonly css_button_small" onclick="top.restoreSession()"> <span>{xl t="View Patient"}</span></a></td></tr></table></p>
<form name="vitals" method="post" action="{$FORM_ACTION}/interface/forms/vitals/save.php" onSubmit="return vitalsFormSubmitted()">
<div id="chart" class="chart-dygraphs"></div>
<table>
	<tr><th align="left">{xl t="Name"}</th><th align="left">{xl t="Unit"}</th>
	<th class='currentvalues' title='{xl t='Date and time of this observation'}'>
    <input type='text' size='14' class='datetimepicker' name='date' id='date'
     value='{$vitals->get_date()|date_format:"%Y-%m-%d %H:%M"}' />
</th>
	{foreach item=result from=$results}
		<th class='historicalvalues'>{$result.date|date_format:"%Y-%m-%d %H:%M"}</th>
	{/foreach}</tr>

	{if $units_of_measurement == 4}<tr class="hide">{else}<tr>{/if}
		{if $units_of_measurement == 2}<td class="unfocus graph" id="weight">{else}<td class="graph" id="weight">{/if}{xl t="Weight"}</td>
		{if $units_of_measurement == 2}<td class="unfocus">{else}<td>{/if}{xl t="lbs"}</td>
		{if $units_of_measurement == 2}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' name='weight' id='weight_input' value="{if $vitals->get_weight() != 0}{$vitals->get_weight()}{/if}" onChange="convLbtoKg('weight_input');" title='{xl t="Decimal pounds or pounds and ounces separated by #(e.g. 5#4)"}'/>
                </td>
	{foreach item=result from=$results}
		<td  class='historicalvalues'>{$vitals->display_weight($result.weight)}</td>
	{/foreach}</tr>

	{if $units_of_measurement == 3}<tr class="hide">{else}<tr>{/if}
                {if $units_of_measurement == 1}<td class="unfocus graph" id="weight_metric">{else}<td class="graph" id="weight_metric">{/if}{xl t="Weight"}</td>
		{if $units_of_measurement == 1}<td class="unfocus">{else}<td>{/if}{xl t="kg"}</td>
                {if $units_of_measurement == 1}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' id='weight_input_metric' value="{if $vitals->get_weight() != 0}{math equation="number * constant" number=$vitals->get_weight() constant=0.45359237 format="%.2f"}{/if}" onChange="convKgtoLb('weight_input');"/>
                </td>
        {foreach item=result from=$results}
                <td  class='historicalvalues'>{if $result.weight != 0}{math equation="number * constant" number=$result.weight constant=0.45359237 format="%.2f"}{/if}</td>
        {/foreach}</tr>

	{if $units_of_measurement == 4}<tr class="hide">{else}<tr>{/if}
	        {if $units_of_measurement == 2}<td class="unfocus graph" id="height">{else}<td class="graph" id="height">{/if}{xl t="Height/Length"}</td>
		{if $units_of_measurement == 2}<td class="unfocus">{else}<td>{/if}{xl t="in"}</td>
		{if $units_of_measurement == 2}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' name='height' id='height_input' value="{if $vitals->get_height() != 0}{$vitals->get_height()}{/if}" onChange="convIntoCm('height_input');"/>
                </td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.height != 0}{$result.height}{/if}</td>
	{/foreach}</tr>

	{if $units_of_measurement == 3}<tr class="hide">{else}<tr>{/if}
                {if $units_of_measurement == 1}<td class="unfocus graph" id="height_metric">{else}<td class="graph" id="height_metric">{/if}{xl t="Height/Length"}</td>
		{if $units_of_measurement == 1}<td class="unfocus">{else}<td>{/if}{xl t="cm"}</td>
                {if $units_of_measurement == 1}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' id='height_input_metric' value="{if $vitals->get_height() != 0}{math equation="round(number * constant,1)" number=$vitals->get_height() constant=2.54 format="%.2f"}{/if}" onChange="convCmtoIn('height_input');"/>
                </td>
        {foreach item=result from=$results}
                <td class='historicalvalues'>{if $result.height != 0}{math equation="number * constant" number=$result.height constant=2.54 format="%.2f"}{/if}</td>
        {/foreach}</tr>

	<tr><td class="graph" id="bps">{xl t="BP Systolic"}</td><td>{xl t="mmHg"}</td>
		<td class='currentvalues'><input type="text" size='5'
			name='bps' id='bps_input' value="{$vitals->get_bps()}"/></td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{$result.bps}</td>
	{/foreach}</tr>

	<tr><td class="graph" id="bpd">{xl t="BP Diastolic"}</td><td>{xl t="mmHg"}</td>
		<td class='currentvalues'><input type="text" size='5'
			name='bpd' id='bpd_input' value="{$vitals->get_bpd()}"/></td>
	{foreach item=result from=$results}
		<td  class='historicalvalues'>{$result.bpd}</td>
	{/foreach}</tr>

	<tr><td class="graph" id="pulse">{xl t="Pulse"}</td><td>{xl t="per min"}</td>
			<td class='currentvalues'><input type="text" size='5'
			name='pulse' id='pulse_input' value="{if $vitals->get_pulse() != 0}{$vitals->get_pulse()|string_format:"%.0f"}{/if}"/></td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.pulse != 0}{$result.pulse|string_format:"%.0f"}{/if}</td>
	{/foreach}</tr>

	<tr><td class="graph" id="respiration">{xl t="Respiration"}</td><td>{xl t="per min"}</td>
		<td class='currentvalues'><input type="text" size='5'
			name='respiration' id='respiration_input' value="{if $vitals->get_respiration() != 0}{$vitals->get_respiration()|string_format:"%.0f"}{/if}"/></td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.respiration != 0}{$result.respiration|string_format:"%.0f"}{/if}</td>
	{/foreach}</tr>

	{if $units_of_measurement == 4}<tr class="hide">{else}<tr>{/if}
	        {if $units_of_measurement == 2}<td class="unfocus graph" id="temperature">{else}<td class="graph" id="temperature">{/if}{xl t="Temperature"}</td>
		{if $units_of_measurement == 2}<td class="unfocus">{else}<td>{/if}{xl t="F"}</td>
		{if $units_of_measurement == 2}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' name='temperature' id='temperature_input' value="{if $vitals->get_temperature() != 0}{$vitals->get_temperature()}{/if}" onChange="convFtoC('temperature_input');"/>
                </td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.temperature != 0}{$result.temperature}{/if}</td>
	{/foreach}</tr>

	{if $units_of_measurement == 3}<tr class="hide">{else}<tr>{/if}
                {if $units_of_measurement == 1}<td class="unfocus graph" id="temperature_metric">{else}<td class="graph" id="temperature_metric">{/if}{xl t="Temperature"}</td>
		{if $units_of_measurement == 1}<td class="unfocus">{else}<td>{/if}{xl t="C"}</td>
                {if $units_of_measurement == 1}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' id='temperature_input_metric' value="{if $vitals->get_temperature() != 0}{math equation="(number - constant2 ) * constant" number=$vitals->get_temperature() constant=0.5556 constant2=32 format="%.2f"}{/if}" onChange="convCtoF('temperature_input');"/>
                </td>
        {foreach item=result from=$results}
                <td class='historicalvalues'>{if $result.temperature != 0}{math equation="(number - constant2 ) * constant" number=$result.temperature constant=0.5556 constant2=32 format="%.2f"}{/if}</td>
        {/foreach}</tr>

	<tr><td>{xl t="Temp Location"}<td></td></td>
			<td class='currentvalues'><select name="temp_method" id='temp_method'/><option value=""> </option>
			<option value="Oral"              {if $vitals->get_temp_method() == "Oral"              || $vitals->get_temp_method() == 2 } selected{/if}>{xl t="Oral"}
			<option value="Tympanic Membrane" {if $vitals->get_temp_method() == "Tympanic Membrane" || $vitals->get_temp_method() == 1 } selected{/if}>{xl t="Tympanic Membrane"}
			<option value="Rectal"            {if $vitals->get_temp_method() == "Rectal"            || $vitals->get_temp_method() == 3 } selected{/if}>{xl t="Rectal"}
			<option value="Axillary"          {if $vitals->get_temp_method() == "Axillary"          || $vitals->get_temp_method() == 4 } selected{/if}>{xl t="Axillary"}
			<option value="Temporal Artery"   {if $vitals->get_temp_method() == "Temporal Artery" } selected{/if}>{xl t="Temporal Artery"}
		</select></td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.temp_method}{xl t=$result.temp_method}{/if}</td>
	{/foreach}</tr>

	<tr><td class="graph" id="oxygen_saturation">{xl t="Oxygen Saturation"}</td><td>{xl t="%"}</td>
		<td class='currentvalues'><input type="text" size='5'
			name='oxygen_saturation' id='oxygen_saturation_input' value="{if $vitals->get_oxygen_saturation() != 0}{$vitals->get_oxygen_saturation()|string_format:"%.0f"}{/if}"/></td>
	{foreach item=result from=$results}
		<td  class='historicalvalues'>{if $result.oxygen_saturation != 0}{$result.oxygen_saturation|string_format:"%.0f"}{/if}</td>
	{/foreach}</tr>

	{if $units_of_measurement == 4 || $gbl_vitals_options > 0}<tr class="hide">{else}<tr>{/if}
	        {if $units_of_measurement == 2}<td class="unfocus graph" id="head_circ">{else}<td class="graph" id="head_circ">{/if}{xl t="Head Circumference"}</td>
		{if $units_of_measurement == 2}<td class="unfocus">{else}<td>{/if}{xl t="in"}</td>
		{if $units_of_measurement == 2}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' name='head_circ' id='head_circ_input' value="{if $vitals->get_head_circ() != 0}{$vitals->get_head_circ()}{/if}" onChange="convIntoCm('head_circ_input');"/>
                </td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.head_circ != 0}{$result.head_circ}{/if}</td>
	{/foreach}</tr>

	{if $units_of_measurement == 3 || $gbl_vitals_options > 0}<tr class="hide">{else}<tr>{/if}
                {if $units_of_measurement == 1}<td class="unfocus graph" id="head_circ_metric">{else}<td class="graph" id="head_circ_metric">{/if}{xl t="Head Circumference"}</td>
		{if $units_of_measurement == 1}<td class="unfocus">{else}<td>{/if}{xl t="cm"}</td>
                {if $units_of_measurement == 1}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' id='head_circ_input_metric' value="{if $vitals->get_head_circ() != 0}{math equation="round(number * constant,1)" number=$vitals->get_head_circ() constant=2.54 format="%.2f"}{/if}" onChange="convCmtoIn('head_circ_input');"/>
                </td>
        {foreach item=result from=$results}
                <td class='historicalvalues'>{if $result.head_circ != 0}{math equation="number * constant" number=$result.head_circ constant=2.54 format="%.2f"}{/if}</td>
        {/foreach}</tr>

	{if $units_of_measurement == 4 || $gbl_vitals_options > 0}<tr class="hide">{else}<tr>{/if}
	        {if $units_of_measurement == 2}<td class="unfocus graph" id="waist_circ">{else}<td class="graph" id="waist_circ">{/if}{xl t="Waist Circumference"}</td>
		{if $units_of_measurement == 2}<td class="unfocus">{else}<td>{/if}{xl t="in"}</td>
		{if $units_of_measurement == 2}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' name='waist_circ' id='waist_circ_input' value="{if $vitals->get_waist_circ() != 0}{$vitals->get_waist_circ()}{/if}" onChange="convIntoCm('waist_circ_input');"/>
                </td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.waist_circ != 0}{$result.waist_circ}{/if}</td>
	{/foreach}</tr>

	{if $units_of_measurement == 3 || $gbl_vitals_options > 0}<tr class="hide">{else}<tr>{/if}
                {if $units_of_measurement == 1}<td class="unfocus graph" id="waist_circ_metric">{else}<td class="graph" id="waist_circ_metric">{/if}{xl t="Waist Circumference"}</td>
		{if $units_of_measurement == 1}<td class="unfocus">{else}<td>{/if}{xl t="cm"}</td>
                {if $units_of_measurement == 1}<td class="valuesunfocus">{else}<td class='currentvalues'>{/if}
                <input type="text" size='5' id='waist_circ_input_metric' value="{if $vitals->get_waist_circ() != 0}{math equation="round(number * constant,1)" number=$vitals->get_waist_circ() constant=2.54 format="%.2f"}{/if}" onChange="convCmtoIn('waist_circ_input');"/>
                </td>
        {foreach item=result from=$results}
                <td class='historicalvalues'>{if $result.waist_circ != 0}{math equation="number * constant" number=$result.waist_circ constant=2.54 format="%.2f"}{/if}</td>
        {/foreach}</tr>

	<tr><td class="graph" id="BMI">{xl t="BMI"}</td><td>{xl t="kg/m^2"}</td>
		<td class='currentvalues'><input type="text" size='5'
			name='BMI' id='BMI_input' value="{if $vitals->get_BMI() != 0}{$vitals->get_BMI()|substr:0:5}{/if}"/></td>
	{foreach item=result from=$results}
		<td class='historicalvalues'>{if $result.BMI != 0}{$result.BMI|substr:0:5}{/if}</td>
	{/foreach}</tr>

  <tr><td>{xl t="BMI Status"}</td><td>{xl t="Type"}</td>
    <td class='currentvalues'><input type="text" size='15'
    name="BMI_status" id="BMI_status' value="{$vitals->get_BMI_status()}"/></td>
  {foreach item=result from=$results}
    <td  class='historicalvalues'>{if $result.BMI_status}{xl t=$result.BMI_status}{/if}</td>
  {/foreach}
  </tr>

  <tr><td>{xl t="Other Notes"}<td></td></td>
    <td class='currentvalues'><input type="text" size='20'
      name="note" id='note' value="{$vitals->get_note()}" /></td>
  {foreach item=result from=$results}
    <td class='historicalvalues'>{$result.note}</td>
  {/foreach}</tr>

	<tr>
	<td colspan='3' style='text-align:center'>
	{if $patient_age <= 20 || (preg_match('/month/', $patient_age))}
	<!-- only show growth-chart button for patients < 20 years old -->
	<!-- <input type="button" id="growthchart" value="{xl t="Growth-Chart"}" style='margin-left: 20px;'> -->
	<input type="button" id="pdfchart" value="{xl t="Growth-Chart"} ({xl t="PDF"})" style='margin-left: 20px;'>
        <input type="button" id="htmlchart" value="{xl t="Growth-Chart"} ({xl t="HTML"})" style='margin-left: 20px;'>
	{/if}
	</td>
	</tr>

	<tr><td colspan='3' style='text-align:center'>&nbsp;</td></tr>
	<tr>
	<td colspan='3' style='text-align:center'>
        <input type="submit" class="editonly" name="Submit" value="{xl t="Save Form"}">
        <input type="button" class="editonly" id="cancel" value="{xl t="Don't Save"}">
        </td>
</tr>
</table>
<br><br>
<input type="hidden" name="id" id='id' value="{$vitals->get_id()}" />
<input type="hidden" name="activity" id='activity' value="{$vitals->get_activity()}">
<input type="hidden" name="pid" id='pid' value="{$vitals->get_pid()}">
<input type="hidden" name="process" id='process' value="true">
</form>
</body>

<script language="javascript">
var formdate = '{$vitals->get_date()|date_format:"%Y%m%d"}';
// vitals array elements are in the format:
//   date-height-weight-head_circumference
var vitals = new Array();
// get values from the current form elements
vitals[0] = formdate+'-{$vitals->get_height()}-{$vitals->get_weight()}-{$vitals->get_head_circ()}';
// historic values
{foreach item=result from=$results}
vitals[vitals.length] = '{$result.date|date_format:"%Y%m%d"}-{$result.height}-{$result.weight}-{$result.head_circ}';
{/foreach}
var patientAge='{$patient_age}';
var patient_dob='{$patient_dob}';
var webroot = '{$FORM_ACTION}';
var pid = '{$vitals->get_pid()}';
var cancellink = '{$DONT_SAVE_LINK}';
var birth_xl='{xl t="Birth-24 months"}'
var older_xl='{xl t="2-20 years"}';
{literal}
function addGCSelector()
{
    var options=new Array();
    var birth={'display':birth_xl,'param':'birth'};
    var age2={'display':older_xl,'param':'2-20'}
    if((patientAge.indexOf('24 month')>=0) || (patientAge.indexOf('month')==-1))
        {
            var dob_data=patient_dob.split("-");
            var dob_date=new Date(dob_data[0],parseInt(dob_data[1])-1,dob_data[2]);
            options[0]=age2;
            for(var idx=0;idx<vitals.length;idx++)
                {
                    var str_data_date=vitals[idx].split("-")[0];
                    var data_date=new Date(str_data_date.substr(0,4),parseInt(str_data_date.substr(4,2))-1,str_data_date.substr(6,2));
                    if(((data_date-dob_date)/86400000)<=2*365)
                        {
                            idx=vitals.length;
                            options[1]=birth
                        }
                }
        }
        else
        {
            options[0]=birth;
        }
        var chart_buttons_cell=$("#pdfchart").parent("td");
        var select=$("<select id='chart_type'></select>");
        chart_buttons_cell.prepend(select);
        for(idx=0;idx<options.length;idx++)
            {
                var option=$("<option value='"+options[idx].param+"'>"+options[idx].display+"</option>");
                select.append(option);
            }
        select.find("option:first").attr("selected","true");
        if(options.length<2)
            {
                select.css("display","none");
            }
}

$(document).ready(function(){
    $("#growthchart").click(function() { ShowGrowthchart(); });
    $("#pdfchart").click(function() { ShowGrowthchart(1); });
    $("#htmlchart").click(function() { ShowGrowthchart(2); });
    $("#cancel").click(function() { location.href=cancellink; });
    addGCSelector();

    $('.datetimepicker').datetimepicker({
        {/literal}{php} $datetimepicker_timepicker = true; {/php}
        {php} $datetimepicker_showseconds = false; {/php}
        {php} $datetimepicker_formatInput = false; {/php}
        {php} require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); {/php}
        {php} // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma {/php}{literal}
    });

});

function ShowGrowthchart(doPDF) {
    // get values from the current form elements
    {/literal}
    vitals[0] = formdate+'-'+$("#height_input").val()+'-'+$("#weight_input").val()+'-'+$("#head_circ_input").val();
    {literal}
    // build the data string
    var datastring = "";
    for(var i=0; i<vitals.length; i++) {
        datastring += vitals[i]+"~";
    }
    newURL = webroot+'/interface/forms/vitals/growthchart/chart.php?pid='+pid+'&data='+datastring;
    if (doPDF == 1) newURL += "&pdf=1";
    if (doPDF == 2) newURL += "&html=1";
    newURL+="&chart_type="+$("#chart_type").val();
    // do the new window stuff
    top.restoreSession();
    window.open(newURL, '_blank', "menubar=1,toolbar=1,scrollbars=1,resizable=1,width=600,height=450");
}

function convLbtoKg(name) {
    var lb = $("#"+name).val();
    var hash_loc=lb.indexOf("#");
    if(hash_loc>=0)
    {
        var pounds=lb.substr(0,hash_loc);
        var ounces=lb.substr(hash_loc+1);
        var num=parseInt(pounds)+parseInt(ounces)/16;
        lb=num;
        $("#"+name).val(lb);
    }
    if (lb == "0") {
        $("#"+name+"_metric").val("0");
    }
    else if (lb == parseFloat(lb)) {
	kg = lb*0.45359237;
        kg = kg.toFixed(2);
        $("#"+name+"_metric").val(kg);
    }
    else {
        $("#"+name+"_metric").val("");
    }

    if (name == "weight_input") {
        calculateBMI();
    }
}

function convKgtoLb(name) {
    var kg = $("#"+name+"_metric").val();

    if (kg == "0") {
        $("#"+name).val("0");
    }
    else if (kg == parseFloat(kg)) {
        lb = kg/0.45359237;
        lb = lb.toFixed(2);
        $("#"+name).val(lb);
    }
    else {
        $("#"+name).val("");
    }

    if (name == "weight_input") {
        calculateBMI();
    }
}

function convIntoCm(name) {
    var inch = $("#"+name).val();

    if (inch == "0") {
        $("#"+name+"_metric").val("0");
    }
    else if (inch == parseFloat(inch)) {
        cm = inch*2.54;
        cm = cm.toFixed(2);
        $("#"+name+"_metric").val(cm);
    }
    else {
        $("#"+name+"_metric").val("");
    }

    if (name == "height_input") {
        calculateBMI();
    }
}

function convCmtoIn(name) {
    var cm = $("#"+name+"_metric").val();

    if (cm == "0") {
        $("#"+name).val("0");
    }
    else if (cm == parseFloat(cm)) {
        inch = cm/2.54;
        inch = inch.toFixed(2);
        $("#"+name).val(inch);
    }
    else {
        $("#"+name).val("");
    }

    if (name == "height_input") {
        calculateBMI();
    }
}

function convFtoC(name) {
    var Fdeg = $("#"+name).val();
    if (Fdeg == "0") {
        $("#"+name+"_metric").val("0");
    }
    else if (Fdeg == parseFloat(Fdeg)) {
        Cdeg = (Fdeg-32)*0.5556;
        Cdeg = Cdeg.toFixed(2);
        $("#"+name+"_metric").val(Cdeg);
    }
    else {
        $("#"+name+"_metric").val("");
    }
}

function convCtoF(name) {
    var Cdeg = $("#"+name+"_metric").val();
    if (Cdeg == "0") {
        $("#"+name).val("0");
    }
    else if (Cdeg == parseFloat(Cdeg)) {
        Fdeg = (Cdeg/0.5556)+32;
        Fdeg = Fdeg.toFixed(2);
        $("#"+name).val(Fdeg);
    }
    else {
        $("#"+name).val("");
    }
}

function calculateBMI() {
    var bmi = 0;
    var height = $("#height_input").val();
    var weight = $("#weight_input").val();
    if(height == 0 || weight == 0) {
        $("#BMI").val("");
    }
    else if((height == parseFloat(height)) && (weight == parseFloat(weight))) {
        bmi = weight/height/height*703;
        bmi = bmi.toFixed(1);
        $("#BMI_input").val(bmi);
    }
    else {
        $("#BMI_input").val("");
    }
}

</script>
{/literal}

</html>
